



/**
 * Fetches the current position of the user or better the mobile device.
 * And takes care that that position is Shown in the Google map.
 * 
 */
function doShowCurrentGeoPosition() {

    var options = {
        enableHighAccuracy: true,
        timeout: 4000,
        maximumAge: 0
    };

    function success(pos) {

        var crd = pos.coords;

        /**
         console.log('Your current position is: Latitude : ' + crd.latitude 
         + ' Longitude: ' + crd.longitude 
         + ' More or less ' + crd.accuracy + ' meters.');
         **/

        showPositionInGoogleMap(pos);

    }
    ;

    function error(err) {
        console.warn('ERROR(' + err.code + '): ' + err.message);
        alert("Current location couldn't be determined because: " + err.message);
    };
    
    try{
        navigator.geolocation.getCurrentPosition(success, error, options);
    }catch(err){
        error(err);
    }

}



var lastGoogleMapCenterPoint;



/**
 * Displays the given position in the Google Map
 * 
 */
function showPositionInGoogleMap(position) {

//Set the current position as a Google Maps object
    var newPoint = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    
    lastGoogleMapCenterPoint = newPoint;

    $('#mapId').gmap({'center': newPoint, 'callback': function() {

            //$('#mapId').gmap({'zoom':16});

            $('#mapId').gmap('clearMarkers');

            $('#mapId').gmap('addMarker', {'bounds': true, 'position': newPoint, 'animation': google.maps.Animation.DROP}, function(map, marker) {
                $('#mapId').gmap('addInfoWindow', {'position': marker.getPosition(), 'content': 'Your are here!'}, function(iw) {
                    $(marker).click(function() {
                        iw.open(map, marker);
                        map.panTo(marker.getPosition());
                    });
                });
            });

            addToLocalStorage(getListEntryText(position));

            addToPreviousLocationsViewItem(getListEntryText(position));


        }
    }
    );

}




/**
 * Adds the location to the browser data storage.
 * And the list in the users browser view.
 * 
 */
function addToPreviousLocationsViewItem(textString) {

    $('#PreviousLocationsContentListId').append(textString).trigger('create');

}



function getListEntryText(position) {

    return '<li data-role="list-divider"> <p>' + getListEntry(position) + '</p></li>';

}



function getNowString() {

    return new Date().toGMTString();

}



function getListEntry(position) {

    return getNowString()
            + ' <br>&nbsp;&nbsp;Lat.: ' + position.coords.latitude
            + ' <br>&nbsp;&nbsp;Long.: ' + position.coords.longitude;
}



function doBuildPreviousLocationsItemInitially() {


    var localStoreEntries = getLocalStorageEntries();
    
    if ( getLocalStorageEntries() !== null ){
        //console.log('doBuild .. ' + localStoreEntries);
        addToPreviousLocationsViewItem(localStoreEntries);
    }
    
    

}




function getGoogleMapHeight(){
    
    //console.info( $( window ).height() );
    
    return $( window ).height() - 184;
    
}



function reHeightGooglemap(){
    
    $("#mapId").height( getGoogleMapHeight() );    
    //google.maps.event.trigger(map, 'resize');
    $('#mapId').gmap({'center': lastGoogleMapCenterPoint });
}


